package cn.jiyun.kafka.consumer;

import cn.jiyun.kafka.pojo.User;
import com.alibaba.fastjson.JSON;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class UserListener {

    @KafkaListener(topics = {"kafka-hello-user"})
    public void receiveMsg(ConsumerRecord<?,?> consumerRecord){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(consumerRecord);
        if(optional.isPresent()){
            //获取value值
            String value = (String) consumerRecord.value();
            //解析value值
            User user = JSON.parseObject(value, User.class);
            System.out.println(user);
        }
    }
}
